<template>
	<div style="height: 100%">
		<zh-tree
			ref="treeRef"
			:height="460"
			show-checkbox
			:default-expanded-keys="state.expandedKeys"
			:default-checked-keys="state.checkedOrgKeys"
			:autoExpandParent="state.autoExpandParent"
			:data="state.orgList"
			:check-strictly="true"
			:props="{ children: 'children', label: 'title' }"
			node-key="key"
		>
		</zh-tree>
		
	</div>
</template>

<script lang="ts" setup name="OrgTreeForm">
import { ElTree } from 'element-plus';

//------------------接收传入参数(组件)-------------------
const props = defineProps({
	type: String,
	id: String,
});

//------------------定义子组件---------------------------
const treeRef = ref<InstanceType<typeof ElTree>>();

//------------------内部变量定义-------------------------
const state = reactive({
	description: '组织树',
	// 组织树
	expandedKeys: [],
	// selectedKeys: [],
	searchValue: '',
	autoExpandParent: true,
	orgList: [] as any,
	checkedOrgKeys: [],
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue']);

//------------------定义调用方法-------------------------
function loopExpandKey(data, keys, expandKeys) {
	let flag = false;
	data.forEach((item) => {
		let itemFlag = false;
		const orgId = item.key;
		keys.some((item) => {
			if (orgId === item) {
				itemFlag = true;
				return true;
			}
		});
		if (item.children) {
			const childFlag = loopExpandKey(item.children, keys, expandKeys);
			if (childFlag) {
				itemFlag = true;
			}
		}
		if (itemFlag) {
			flag = true;
			expandKeys.push(item.key);
		}
	});
	return flag;
}
function findOrgTreeList() {
	const params: any = {
		searchText: state.searchValue,
	};
	if (props.type === 'user') {
		params.userId = props.id;
	} else if (props.type === 'role') {
		params.roleId = props.id;
	}
	setTimeout(() => {
		$api.common.send
			.post('org0009', params)
			.then((res) => {
				const data = [...res.result.treeList];
				if (data) {
					state.orgList = data;
					state.checkedOrgKeys = [];
					const expandedKeys = res.result.expandedKeys;
					const checkedKeys = res.result.checkedKeys;
					if (checkedKeys && checkedKeys.length > 0) {
						// 设置展开节点
						const expandedOrgKeys = [];
						loopExpandKey(data, checkedKeys, expandedOrgKeys);
						state.checkedOrgKeys = checkedKeys;
						state.expandedKeys = expandedOrgKeys;
					} else {
						state.expandedKeys = expandedKeys;
					}
					state.autoExpandParent = true;
				}
			})
			.catch(() => {});
	}, 50);
}

function getCheckedKeys() {
	return treeRef.value!.getCheckedKeys(false);
}
function saveOrg() {
	let funcCode = '';
	const params = {} as any;
	if (props.type === 'role') {
		funcCode = 'role0011';
		params.roleId = props.id;
	} else if (props.type === 'user') {
		funcCode = 'user0020';
		params.userId = props.id;
	}
	if (props.id) {
		const checkedkeys: any = getCheckedKeys();
		if (Array.isArray(checkedkeys)) {
			params.orgIds = checkedkeys;
		} else {
			params.orgIds = checkedkeys.checked;
		}
		setTimeout(() => {
			$api.common.send
				.post(funcCode, params)
				.then(() => {
					$messageBox.success('授权成功', '提示');
					emit('update:modelValue', false);
				})
				.catch(() => {});
		}, 10);
	} else {
		$messageBox.warning('请先选择需要操作的用户', '提示');
	}
}

//------------------初始化------------------------------
onMounted(() => {});

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({
	getCheckedKeys,
	saveOrg,
	findOrgTreeList,
});
</script>
